<?php

/**
 * Class Dispatcher
 * 应用分发
 */
class Dispatcher
{
    public static function run($config){
        $groupConfig = $config["group"];

        unset($config["group"]);

        $routeStr = ltrim($_GET["r"],'/');

        // 使用main.php中定义的默认路由
        if (!$routeStr){
            return (new yii\web\Application($config))->run();
        }

        $routeArr = explode('/',$routeStr);

        //分组
        $group = trim(array_shift($routeArr));

        //定义group常量
        defined('GROUP') or define('GROUP', $group);

        if (isset($groupConfig[$group])){

            $_GET["r"] = implode('/',$routeArr);

            (new $groupConfig[$group]($config))->run();
        }else{
            (new yii\web\Application($config))->run();
        }
    }
}
